package com.xxxx.seckill.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * MVC控制类
 */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private UserArgumentResolver userArgumentResolver;

    @Autowired
    private AccessLimitInterceptor accessLimitInterceptor;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }

    /**
     * 添加默认的静态资源访问路径，解决访问静态资源报404错误的问题
     * <a href="https://huaweicloud.csdn.net/63876542dacf622b8df8b3e5.html">...</a>
     *
     * @param registry :
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

//        registry.addResourceHandler("/**")
//                .addResourceLocations(
//                        "classpath:static/",
//                        "classpath:META-IFA/resources/",
//                        "classpath:resources/",
//                        "classpath:public/",
//                        "classpath:/"
//                );

        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:static/");

//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:META-IFA/resources/");

//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:resources/");

//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:public/");

//        registry.addResourceHandler("/**")
//                .addResourceLocations("classpath:/");

//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");

//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accessLimitInterceptor);
    }
}
